package spindles.gwt.client;

import java.util.ArrayList;
import java.util.List;

import spindles.gwt.shared.DTO;

import com.google.gwt.user.client.rpc.IsSerializable;

public class TableData implements IsSerializable{
	
	private String tableName;
	
	private String fieldNames[];
	
	private List rows = new ArrayList();
	
	public TableData(String tableName, String fieldNames[]){
		this.tableName = tableName;
		this.fieldNames = fieldNames;
	}
	
	public String[] getFieldNames(){
		return fieldNames;
	}
	
	public int getFieldCount(){
		return fieldNames.length;
	}
	
	public void addRow(Row r){
		rows.add(r);
		r.setTableName(tableName);
		r.setFieldNames(fieldNames);
	}
	
	public void addRow(DTO dto, int position){
		addRow(new Row(dto, position));
	}
	
	public Row getRow(int index) {
		if (index >= rows.size()) {
			return null;
		}
		return (Row) rows.get(index);
	}
	
	public int getRowCount() {
		return rows.size();
	}
	
	public static class Row {		
		private String tableName;
		private String fieldNames[];
		private DTO dto;
		private int position;
		
		public Row(DTO dto, int position){
			this.dto = dto;			
			this.position = position;
		}
		
		public DTO getDTO(){
			return dto;
		}
		
		public int getRowPosition(){
			return position;
		}
		
		public String getFieldName(int i){
			return fieldNames[i];
		}
		
		public void setFieldNames(String fieldNames[]){
			this.fieldNames = fieldNames;
		}
		
		public String getValue(int field){
			return dto.toRecord()[field];
		}
		
		public String getTableName(){
			return this.tableName;
		}

		public void setTableName(String tableName) {
			this.tableName = tableName;
		}
	}
	
	

}
